package cn.vastbase.jdbc.base;

import cn.com.vastbase.PGProperty;
import org.junit.Test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

/**
 * <p> 连接示例 </p>
 * <p>使用Vastbase G100 2.2 Build 15.5 </p>
 */
public class ConnTestCase {

    static Connection conn = null;
    static String cname = "org.postgresql.Driver";
    static String url = "jdbc:postgresql://192.168.56.12:5000/vastbase?loggerLevel=off";
    static String username = "test";
    static String passwd = "Vastdata#0730";

    @Test
    public void connect() {
        try {
            Class.forName(cname);
            conn = DriverManager.getConnection(url, username, passwd);
            System.out.println("[SUCCESS] conn database success.");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 连接示例2 使用Properties设置参数
     */
    @Test
    public void connect2() {
        Properties properties = new Properties();
        PGProperty.USER.set(properties, username);
        PGProperty.PASSWORD.set(properties, passwd);
        try (Connection conn = DriverManager.getConnection(url, properties);
             ResultSet rs = conn.createStatement().executeQuery("select version()")) {
            while (rs.next()) {
                System.out.println(rs.getString(1));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public void disConn(Connection conn) throws SQLException {
        if (conn != null) {
            conn.close();
        }
    }
}
